/**
 * 
 */
package com.wsd.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.Deque;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.wsd.bean.Category;
import com.wsd.bean.ProductComment;
import com.wsd.bean.Products;
import com.wsd.bean.Users;
import com.wsd.dao.ProductCommentDaoInterface;
import com.wsd.dao.impl.ProductCommentDAO;
import com.wsd.service.CategoryService;
import com.wsd.service.ProductService;
import com.wsd.util.PagingBuilder;

/**
 * @author TuZiJun
 *
 */
public class ProductsAction extends ActionSupport {
	private ProductService productService = null;
	private int cid;
	private int pid;
	private int page = 1;
	private String keyWord = null;
	private static final int PAGESIZE = 10;
	private CategoryService categoryService = null;
	private short rankStyle;
	private String content = null;
	
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public void setCid(int cid) {
		this.cid = cid;
	}
	
	public void setKeyWord(String keyWord) {
		try {
			this.keyWord = URLDecoder.decode(keyWord, "UTF-8");
			System.out.println(this.keyWord);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	
	/**
	 * @param content the content to set
	 */
	public void setContent(String content) {
		this.content = content;
	}
	
	/**
	 * @param rankStyle the rankStyle to set
	 */
	public void setRankStyle(short rankStyle) {
		this.rankStyle = rankStyle;
	}

	/**
	 * 显示所有产品信息
	 * @return
	 */
	public String showProducts() {
		HttpServletRequest request = ServletActionContext.getRequest();
		List<Products> products = productService.findOnePage(page, PAGESIZE);
		request.setAttribute("products", products);
		int max = productService.getTotalPages(PAGESIZE);
		int pagerange = max > 10 ? 10 : max;
		request.setAttribute("pageuri", PagingBuilder.build("/wsd/showproducts.action?page=@page@", page, pagerange, max));
		return SUCCESS;
	}
	/**
	 * 显示用户查询的相关产品信息
	 * @return
	 */
	public String searchProducts() {
		HttpServletRequest request = ServletActionContext.getRequest();
		List<Products> products = productService.searchProducts(page, PAGESIZE, keyWord);
		request.setAttribute("products", products);
		int max = productService.getTotalPages(PAGESIZE, keyWord);
		int pagerange = max > 10 ? 10 : max;
		try {
			request.setAttribute("pageuri", PagingBuilder.build("/wsd/searchproducts.action?page=@page@&keyWord=" + URLEncoder.encode(URLEncoder.encode(keyWord, "UTF-8"),"UTF-8"), page, pagerange, max));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 显示某类别的产品信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String categoryProducts() {
		HttpServletRequest request = ServletActionContext.getRequest();
		List<Products> products = productService.categoryProducts(page, PAGESIZE, cid);
		request.setAttribute("products", products);
		Category c = categoryService.findById(cid);
		Set<Category> category = c.getCategories();
		Deque<Category> path = categoryService.getParentCategorys(c);
		request.setAttribute("cates", category);
		request.setAttribute("path", path);
		int max = productService.getTotalPages(PAGESIZE, cid);
		int pagerange = max > 10 ? 10 : max;
		request.setAttribute("pageuri", PagingBuilder.build("/wsd/categoryproducts.action?page=@page@&cid=" + cid, page, pagerange, max));
		return SUCCESS;
	}
	/**
	 * 显示产品的详细信息
	 * @return
	 */
	public String productDetail() {
		HttpServletRequest request = ServletActionContext.getRequest();
		if(0 == pid)
			pid = (Integer) request.getSession().getAttribute("pid");
		Products product = productService.findById(pid);
		request.setAttribute("product", product);
		ProductComment comment = new ProductComment();
		comment.setProducts(product);
		List<ProductComment> comments = productService.findByComment(comment, page, PAGESIZE);
		request.setAttribute("comments", comments);
		int max = productService.findByCommentCount(comment, PAGESIZE);
		int pagerange = max > 10 ? 10 : max;
		request.setAttribute("pageuri", PagingBuilder.build("/wsd/productDetail.action?page=@page@&pid=" + pid, page, pagerange, max));
		return SUCCESS;
	}
	
	public String comment(){
		Products product = productService.findById(pid);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.getSession().setAttribute("pid", pid);
		Users user = (Users) request.getSession().getAttribute("user");
		if (null == user)
			return LOGIN;
		if (!user.isState()) {
			request.setAttribute("msg", "您的账号当前正在审核中。。。");
			return SUCCESS;
		}
		ProductComment comment = new ProductComment();
		comment.setPCContent(content);
		comment.setProducts(product);
		comment.setUsers(user);
		productService.saveComment(comment);
		return SUCCESS;
	}
	
	public String productRank() {
		HttpServletRequest request = ServletActionContext.getRequest();
		List<Products> recommendProducts = productService.rankByModel((short)1, 5);
		List<Products> hotProducts = productService.rankByModel((short)2, 5);
		List<Products> newProducts = productService.rankByModel((short)3, 5);
		request.setAttribute("recommendProducts", recommendProducts);
		request.setAttribute("hotProducts", hotProducts);
		request.setAttribute("newProducts", newProducts);
		return SUCCESS;
	}
	
	public String productRankDetail() {
		HttpServletRequest request = ServletActionContext.getRequest();
		List<Products> rankProducts = productService.rankProducts(rankStyle, (page - 1) * PAGESIZE, PAGESIZE);
		request.setAttribute("rankProducts", rankProducts);
		String rankName = null;
		switch (rankStyle) {
		case 1:
			rankName = "特价商品";
			break;
		case 2:
			rankName = "热卖商品";
			break;
		case 3:
			rankName = "新品上市";
			break;
		}
		request.setAttribute("rankName", rankName);
		int max = productService.rankProductsCount(rankStyle);
		max = max % PAGESIZE == 0 ? max / PAGESIZE : max / PAGESIZE + 1;
		int pagerange = max > 10 ? 10 : max;
		request.setAttribute("pageuri", PagingBuilder.build("/wsd/productDetail.action?page=@page@&rankStyle=" + rankStyle, page, pagerange, max));
		return SUCCESS;
	}
	
}
